(function () {
    let groups = {
        'v5huichang2': ['a2141.1.v5huichang2.1', 'a2141.1.v5huichang2.2', 'a2141.1.v5huichang2.3', 'a2141.1.v5huichang2.4'],
        'v5biaoqian2': ['a2141.1.v5biaoqian2.5', 'a2141.1.v5biaoqian2.6', 'a2141.1.v5biaoqian2.7', 'a2141.1.v5biaoqian2.8']
    };
    groups.all = [].concat(...Object.values(groups));
    for (let e of groups.all) {
        groups[e] = [e];
    }
    //console.log(groups);
    let keys = ['spm_cnt', 'spm_id', 'pre_spm_id'];
    let steps = [
        [1, 2],
        [0],
        [1],
        [2]
    ];
    let flatGroups = [].concat(...Object.entries(groups).map(([k, v]) => ({
        group: k,
        value: v.map(s => `'${s}'`).join(',')
    })));

    flatGroups = [].concat(...flatGroups.map(group => steps.map(step => Object.assign({}, group, {
        step
    }))));

    console.log(flatGroups);

    let out = flatGroups.map(({
            group,
            value,
            step
        }) =>
        `
SELECT 
    ttid,spm_cnt,spm_id,pre_spm_id,app_id,app_version,visitor_id,event_id,user_id,bucket_id,step,item_id,seller_id,
    '${group}' as spm_group,
    '${step.join(',')}' as spm_group_step
FROM
    template_analyze.ranger_bucket_track_hi
where 
    ds='\${bizdate}' and hh='\${hh}'
    and (
        ${
            step.map(i => keys[i]).map(key => `${key} in (${value})`)
            .join('\n OR \n')
        }
    )
	`
    ).join('\nUNION\n');
    console.log(out);
})();